package com.iqiyi.pps.epg.core.model.reservation;

import com.iqiyi.pps.epg.api.model.web.reservation.TReservation;
import org.apache.commons.lang.StringUtils;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import javax.persistence.*;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

@Entity
@Table(name = "reserve_channel")
@Cache(usage = CacheConcurrencyStrategy.NONE)
public class ReserveControl {
    public static final int TYPE_BROADCAST = 1;     //设置预约播控
    public static final int TYPE_CHANNEL_NAME = 2;  //设置预约改名

    public static final int STATUS_ENABLE_FAIL = 2;     // 生效失败
    public static final int STATUS_ENABLE_SUCCESS = 3;  // 生效成功
    public static final int STATUS_RENEW_FAIL = 4;      // 还原失败
    public static final int STATUS_RENEW_SUCCESS = 5;   // 还原成功
    public static final int STATUS_DELETE = 9;          // 删除

    public static final int OPSSTATUS_ONLINE = 1;       // 上线
    public static final int OPSSTATUS_OFFLINE = 2;      // 下线

    public static final int REJECT_SEARCH_FALSE = 0;
    public static final int REJECT_SEARCH_TRUE = 1;

    public static final Map<Integer, String> TYPE_MAP = new HashMap(){{
        put(1, "播控设置");
        put(2, "频道更名");
    }};

    public static final Map<Integer, String> STATUS_MAP = new HashMap(){{
        put(1, "等待中");
        put(2, "生效失败");
        put(3, "生效成功");
        put(4, "还原失败");
        put(5, "还原成功");
        put(9, "删除");
    }};

    public static final Map<Integer, String> OPSSTATUS_MAP = new HashMap(){{
        put(1, "上线");
        put(2, "下线");
    }};

    private Long id;
    private Integer type;
    private Long fatherId;
    private Integer platform;
    private Timestamp startTime;
    private Timestamp endTime;
    private Integer delayTime;
    private Integer rejectSearch;
    private String blacklist;
    private Integer opsStatus;
    private String reason;
    private Integer status;
    private Timestamp addTime = new Timestamp(System.currentTimeMillis());
    private Timestamp updateTime;

    private int regionRejectSearch;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Integer getType() {
        return type;
    }

    public void setType(Integer type) {
        this.type = type;
    }

    @Column(name = "father_id")
    public Long getFatherId() {
        return fatherId;
    }

    public void setFatherId(Long fatherId) {
        this.fatherId = fatherId;
    }

    public Integer getPlatform() {
        return platform;
    }

    public void setPlatform(Integer platform) {
        this.platform = platform;
    }

    @Column(name = "start_time")
    public Timestamp getStartTime() {
        return startTime;
    }

    public void setStartTime(Timestamp startTime) {
        this.startTime = startTime;
    }

    @Column(name = "end_time")
    public Timestamp getEndTime() {
        return endTime;
    }

    public void setEndTime(Timestamp endTime) {
        this.endTime = endTime;
    }

    @Column(name = "delay_time")
    public Integer getDelayTime() {
        return delayTime;
    }

    public void setDelayTime(Integer delayTime) {
        this.delayTime = delayTime;
    }

    @Column(name = "reject_search")
    public Integer getRejectSearch() {
        return rejectSearch;
    }

    public void setRejectSearch(Integer rejectSearch) {
        this.rejectSearch = rejectSearch;
    }

    @Column(length = 1024)
    public String getBlacklist() {
        return blacklist;
    }

    public void setBlacklist(String blacklist) {
        this.blacklist = blacklist;
    }

    @Column(name = "ops_status")
    public Integer getOpsStatus() {
        return opsStatus;
    }

    public void setOpsStatus(Integer opsStatus) {
        this.opsStatus = opsStatus;
    }

    @Column(length = 48)
    public String getReason() {
        return reason;
    }

    public void setReason(String reason) {
        this.reason = reason;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    @Column(name = "add_time", updatable = false)
    public Timestamp getAddTime() {
        return addTime;
    }

    public void setAddTime(Timestamp addTime) {
        this.addTime = addTime;
    }

    @Column(name = "update_time", insertable = false, updatable = false)
    public Timestamp getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Timestamp updateTime) {
        this.updateTime = updateTime;
    }

    @Column(name = "region_reject_search")
    public int getRegionRejectSearch() {
        return regionRejectSearch;
    }

    public void setRegionRejectSearch(int regionRejectSearch) {
        this.regionRejectSearch = regionRejectSearch;
    }

    public TReservation entityToThrift() {
        TReservation entity = new TReservation();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        entity.setId(this.getId());
        entity.setType(this.getType());
        entity.setFatherId(this.getFatherId());
        entity.setPlatform(this.getPlatform());
        if (this.getStartTime() != null) {
            entity.setStartTime(sdf.format(this.getStartTime()));
        }
        if (this.getEndTime() != null) {
            entity.setEndTime(sdf.format(this.getEndTime()));
        }
        entity.setDelayTime(this.getDelayTime());
        entity.setRejectSearch(this.getRejectSearch());
        entity.setOpsStatus(this.getOpsStatus());
        entity.setBlacklist(this.getBlacklist());
        entity.setReason(this.getReason());
        entity.setStatus(this.getStatus());
        entity.setRegionRejectSearch(this.getRegionRejectSearch());
        if (this.getAddTime() != null) {
            entity.setAddTime(sdf.format(this.getAddTime()));
        }
        if (this.getUpdateTime() != null) {
            entity.setUpdateTime(sdf.format(this.getUpdateTime()));
        }
        return entity;
    }

    public static ReserveControl thriftToEntity(TReservation entity) {
        ReserveControl model = new ReserveControl();
        if (entity.getId() > 0) {
            model.setId(entity.getId());
        }
        if (entity.getType() > 0) {
            model.setType(entity.getType());
        }
        if (entity.getFatherId() > 0) {
            model.setFatherId(entity.getFatherId());
        }
        model.setPlatform(entity.getPlatform());
        if (StringUtils.isNotEmpty(entity.getStartTime())) {
            model.setStartTime(Timestamp.valueOf(entity.getStartTime()));
        }
        if (StringUtils.isNotEmpty(entity.getEndTime())) {
            model.setEndTime(Timestamp.valueOf(entity.getEndTime()));
        }
        model.setDelayTime(entity.getDelayTime());
        model.setRejectSearch(entity.getRejectSearch());
        model.setBlacklist(entity.getBlacklist());
        model.setRegionRejectSearch(entity.getRegionRejectSearch());
        if (entity.getOpsStatus() == 1 || entity.getOpsStatus() == 2) {
            model.setOpsStatus(entity.getOpsStatus());
        } else {
            model.setOpsStatus(1);
        }
        if (StringUtils.isNotEmpty(entity.getReason())) {
            model.setReason(entity.getReason());
        }
        if (entity.getStatus() > 0) {
            model.setStatus(entity.getStatus());
        } else {
            model.setStatus(1);
        }
        if (entity.getAddTime() != null) {
            model.setAddTime(Timestamp.valueOf(entity.getAddTime()));
        }
        if (entity.getUpdateTime() != null) {
            model.setUpdateTime(Timestamp.valueOf(entity.getUpdateTime()));
        }
        return model;
    }
}
